package sample;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import oims.OrderRecord;
import oims.OrderRecordItem;
import pims.ProductRecord;
import sims.StockRecord;
import cims.CustomerRecord;

public class SampleServlet extends HttpServlet {
	/**
	 *
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		perform(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		perform(req, resp);
	}

	private void perform(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");

		String type = req.getParameter("type");
		if ("顧客照会".equals(type)) {
			customer(req, resp);
		}
		else if ("商品照会".equals(type)) {
			product(req, resp);
		}
		else if ("在庫照会".equals(type)) {
			stock(req, resp);
		}
		else if ("注文登録".equals(type)) {
			order(req, resp);
		}
		else if ("配送状況照会".equals(type)) {
			delivery(req, resp);
		}
		else {
		}

		RequestDispatcher dispatcher = req.getRequestDispatcher("result.jsp");
		dispatcher.forward(req, resp);
	}
	private void customer(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String customer = req.getParameter("customer");
		CustomerRecord cr = new CustomerRecord();
		Map<String, String> cmap = cr.getInfo(customer);
		req.setAttribute("cmap", cmap);
	}
	private void product(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String product = req.getParameter("product");
		ProductRecord pr = new ProductRecord();
		Map<String, String> pmap = pr.getInfo(product);
		req.setAttribute("pmap", pmap);
	}
	private void stock(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String stock = req.getParameter("stock");
		StockRecord sr = new StockRecord();
		int quantity = sr.getInfo(stock);
		req.setAttribute("quantity", new Integer(quantity));
	}
	private void order(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String order_customer = req.getParameter("order_customer");
		if (order_customer != null && order_customer.length() > 0) {
			String order_product_1 = req.getParameter("order_product_1");
			String order_price_1 = req.getParameter("order_price_1");
			String order_quantity_1 = req.getParameter("order_quantity_1");
			String order_product_2 = req.getParameter("order_product_2");
			String order_price_2 = req.getParameter("order_price_2");
			String order_quantity_2 = req.getParameter("order_quantity_2");

			OrderRecordItem[] orders = new OrderRecordItem[2];
			orders[0] = new OrderRecordItem();
			orders[0].setProduct(order_product_1);
			orders[0].setPrice(Integer.parseInt(order_price_1));
			orders[0].setQuantity(Integer.parseInt(order_quantity_1));
			orders[1] = new OrderRecordItem();
			orders[1].setProduct(order_product_2);
			orders[1].setPrice(Integer.parseInt(order_price_2));
			orders[1].setQuantity(Integer.parseInt(order_quantity_2));

			OrderRecord or = new OrderRecord();

			Date now = new Date();
			String numbers[] = or.order(order_customer, now, orders);

			req.setAttribute("numbers", numbers);
		}
	}
	private void delivery(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		List<Map<String, String>> olist = null;
		String order_number = req.getParameter("order_number");
		if (order_number != null && order_number.length() > 0) {
			OrderRecord orec = new OrderRecord();
			olist = orec.getInfo(order_number);
			req.setAttribute("olist", olist);
		}
	}
}
